# -*- coding: utf-8 -*-
# !/usr/bin/env python

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

azkaban_url = "https://azkaban.****.cn/"

user = {
    "user": "*****",
    "passwd": "*****"
}

# 设置调度参数
setting1 = {
    "timepicker": "9:00 AM",
    "datepicker": "08/01/2021",
    "period": 1,
    "period_units": "d",
}
# 实例化浏览器
driver = webdriver.Chrome()

# 发送请求
driver.get(azkaban_url)
# 最大化窗口
driver.find_element_by_id('details-button').click()
driver.find_element_by_id('proceed-link').click()
driver.find_element_by_id('username').send_keys(user['user'])  # user['user']是账号
driver.find_element_by_id('password').send_keys(user['passwd'])  # user['passwd']是密码
driver.find_element_by_id('login-submit').click()


def goto_projects(projects):
    driver.find_element_by_name('searchterm').send_keys(projects)
    driver.find_element_by_xpath("""//*[@id="search-form"]/div[1]/div/span/button""").click()
    driver.find_element_by_link_text(projects).click()


def update_file(file):
    driver.find_element_by_id("""project-upload-btn""").click()
    driver.find_element_by_id("""file""").clear()
    driver.find_element_by_id("""file""").send_keys(file)
    driver.find_element_by_id("""upload-project-btn""").click()


def set_job_schedule(python_name, setting=setting1):
    """设置调度"""
    driver.find_element_by_xpath(f"""//*[@id="{python_name}"]/div/button""").click()
    driver.find_element_by_id("""schedule-btn""").click()
    driver.find_element_by_id("""timepicker""").clear()
    driver.find_element_by_id("""timepicker""").send_keys(setting['timepicker'])
    driver.find_element_by_id("""datepicker""").clear()
    driver.find_element_by_id("""datepicker""").send_keys(setting['datepicker'])
    driver.find_element_by_id("""period""").clear()
    driver.find_element_by_id("""period""").send_keys(setting['period'])
    Select(driver.find_element_by_id("period_units")).select_by_value(setting['period_units'])
    driver.find_element_by_id("""schedule-button""").click()
    time.sleep(1)
    driver.find_element_by_xpath("""//*[@id="azkaban-message-dialog"]/div/div/div[3]/button""").click()
    driver.back()

# 访问项目
goto_projects("my_job")
# 上传压缩文件
update_file("/Users/why/Desktop/projects.zip")
# 设置任务定时
set_job_schedule("bi_data_for_sqlmodel_v1", setting=setting1)
